﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Admin_OrderManager : System.Web.UI.UserControl
{
    private SqlConnection connect;
    private SqlDataAdapter da;
    private SqlCommand cmd;
    private DataSet ds;
    private string connectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["myCon"].ToString();
        connect = new SqlConnection(connectionString);
        connect.Open();
        ds = new DataSet();
        if(!IsPostBack)
        {
            bindData();
        }
    }

    protected void bindData()
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetAllOrderByOrderStatus", connect);
            cmd.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }

    protected void bindDatabyStatus(string status)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetOrderByOrderStatus", connect);
            cmd.Parameters.Add("@oStatus", SqlDbType.VarChar).Value = status;
            cmd.CommandType = CommandType.StoredProcedure;
            //cmd.ExecuteNonQuery();
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }
    protected void btnPending_Click(object sender, EventArgs e)
    {
        bindDatabyStatus("pending");
    }
    protected void btnShip_Click(object sender, EventArgs e)
    {
        bindDatabyStatus("Shipped");
    }
    protected void btnDeclined_Click(object sender, EventArgs e)
    {
        bindDatabyStatus("Declined");
    }
    protected void btnAllOrder_Click(object sender, EventArgs e)
    {
        bindData();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        string oid = row.Cells[1].Text;
        string name = row.Cells[2].Text;
        Response.Redirect("AdminOrderDetails.aspx?id="+oid+"&name="+name);
    }
}